from member.models import Retailer, Employee
from wxchat.api.utils import get_openid_from_header
import hashlib


def str2md5(string):
    md5 = hashlib.md5()
    md5.update(string.encode("utf8"))
    result = md5.hexdigest().upper()
    return result


def get_query_set(request, queryset, retailer_id):
    is_exist = Retailer.objects.filter(id=int(retailer_id)).exists()
    if is_exist:
        queryset = queryset.filter(retailer_id=retailer_id)
        return queryset

    openid = get_openid_from_header(request)
    is_exist_emp = Employee.objects.filter(retailer_id=int(retailer_id), openid=openid).exist()
    if is_exist_emp:
        queryset = queryset.filter(openid=openid)
        return queryset

    return None

